//
//  ReportStocks.m
//  iHedron
//
//  Created by George Radev on 2011-08-08.
//  Copyright 2011 I Me Mine. All rights reserved.
//

#import "ReportStocks.h"
#import "HedronCore.h"
#import "OrderStockCell.h"

@implementation ReportStocks

- (void)viewDidLoad {
    [super viewDidLoad];
	self.title = @"Стоки";
}

- (void)viewWillAppear:(BOOL)animated {
    [self.tableView reloadData];
}

#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [[HedronCore getReportSocks] count];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [(NSArray*)[[HedronCore getReportSocks] objectAtIndex: section] count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
	int section = indexPath.section;
	int row = indexPath.row;
	
	static NSString *OrderStockCellIdentifier = @"OrderStockCell";
	OrderStockCell *cell = (OrderStockCell*)[tableView dequeueReusableCellWithIdentifier:OrderStockCellIdentifier];
	if (cell == nil) {
		NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"OrderStockCell" owner:nil options:nil];
		
		for(id currentObject in topLevelObjects){
			if([currentObject isKindOfClass:[UITableViewCell class]]){
				cell = (OrderStockCell*) currentObject;
				break;
			}
		}
	}
	
	// Configure the cell...
	if(cell){
		Stock* stock = (Stock*)[(NSArray*)[[HedronCore getReportSocks] objectAtIndex: section] objectAtIndex: row];
		cell.stockName.text = stock.sname;
		cell.stockQty.text = [NSString stringWithFormat: @"%d", stock.qty];
		cell.stockPrice.text = [NSString stringWithFormat: @"%.2f", stock.price];
	}
	return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	return [HedronCore getReportStocksHeader: section];
}
						   
#pragma mark -

@end
